#include "stdafx.h"
#include "wf_modifydeletefree.h"

#include "pf_point.h"
#include "pf_polyline.h"
#include "pf_modification.h"



WF_ModifyDeleteFree::WF_ModifyDeleteFree(
    PF_Container& container,
    PF_GraphicView& graphicView)
        :PF_ActionInterface("Delete Entities Freehand",
                    container, graphicView) {}

QAction* WF_ModifyDeleteFree::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
/*      QAction* action = new QAction(tr("Delete Freehand"), tr("&Delete Freehand"),
                                  QKeySequence(), NULL); */
    QAction* action = new QAction(tr("Delete Freehand"), NULL);
    //action->zetStatusTip(tr("Delete Freehand"));
    return action;
}


void WF_ModifyDeleteFree::init(int status) {
    PF_ActionInterface::init(status);
    polyline = NULL;
    e1 = e2 = NULL;
    v1 = v2 = PF_Vector(false);
    setSnapMode(LX_Define::SnapOnEntity);
}



void WF_ModifyDeleteFree::trigger() {
    if (e1!=NULL && e2!=NULL) {
        PF_Container* parent = e2->getParent();
        if (parent!=NULL) {
            if (parent->rtti()==LX_Define::EntityPolyline) {
                if(parent->getId() == polyline->getId()) {

                    // deletes whole polyline on screen:
                    graphicView->deleteEntity((PF_Entity*)polyline);

                    // splits up the polyline in the container:
                    PF_Polyline* pl1;
                    PF_Polyline* pl2;
                    PF_Modification m(*container);
                    m.splitPolyline(*polyline,
                                    *e1, v1,
                                    *e2, v2,
                                    &pl1, &pl2);

                    if (document) {
                        document->startUndoCycle();
                        document->addUndoable(polyline);
                        document->addUndoable(pl1);
                        document->addUndoable(pl2);
                        document->endUndoCycle();
                    }

                    // draws the new polylines on the screen:
					graphicView->redraw(LX_Define::RedrawDrawing);

                    init();

                    PF_DIALOGFACTORY->updateSelectionWidget(
                        container->countSelected());
                } else {
        			PF_DIALOGFACTORY->commandMessage(tr("Entities not in the same polyline."));
                }
            } else {
        		PF_DIALOGFACTORY->commandMessage(tr("Parent of second entity is not a polyline"));
            }
        } else {
        	PF_DIALOGFACTORY->commandMessage(tr("Parent of second entity is NULL"));
        }
    } else {
        PF_DIALOGFACTORY->commandMessage(tr("One of the chosen entities is NULL"));
    }
}



void WF_ModifyDeleteFree::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::RightButton) {
        init(getStatus()-1);
    } else {

        switch (getStatus()) {
        case 0: {
                v1 = snapPoint(e);
                e1 = getKeyEntity();
                if (e1!=NULL) {
                    PF_Container* parent = e1->getParent();
                    if (parent!=NULL) {
                        if (parent->rtti()==LX_Define::EntityPolyline) {
                            polyline = (PF_Polyline*)parent;
                            setStatus(1);
                        } else {
        					PF_DIALOGFACTORY->commandMessage(
								tr("Parent of first entity is not a polyline"));
                        }
                    } else {
        				PF_DIALOGFACTORY->commandMessage(
							tr("Parent of first entity is NULL"));
                    }
                } else {
        			PF_DIALOGFACTORY->commandMessage(
						tr("First entity is NULL"));
                }
            }
            break;

        case 1: {
                v2 = snapPoint(e);
                e2 = getKeyEntity();

                if (e2!=NULL) {
                    trigger();
                } else {
        			PF_DIALOGFACTORY->commandMessage(tr("Second entity is NULL"));
                }
            }
            break;
        }
    }
}



void WF_ModifyDeleteFree::updateMouseButtonHints() {
    switch (getStatus()) {
    case 0:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify first break point "
                                               "on a polyline"), tr("Cancel"));
        break;
    case 1:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify second break point "
                                               "on the same polyline"),
                                            tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}

// EOF
